/**
 * 
 */
package com.wsj.web.action.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.common.wsjSecurity.DesEncrypter;
import com.wsj.service.IUserManagerServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.globe.Globals;
import com.wsj.web.action.BaseAction;
import com.wsj.web.form.user.LoginForm;
import com.wsj.web.vo.homeSite.UserBase;

/**
 * @author fengbin  createTime:2009-8-1
 *
 */
public class LoginAction extends BaseAction {

	//用户管理
	private IUserManagerServ  userManagerServ ;

	public IUserManagerServ getUserManagerServ() {
		return userManagerServ;
	}

	public void setUserManagerServ(IUserManagerServ userManagerServ) {
		this.userManagerServ = userManagerServ;
	}
	
	/********
	 * 登入
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward login(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		LoginForm  loginForm=(LoginForm)form;
		String toPage = "loginPage";
		
		try {
			//如果是超级管理员,则登入到后台管理.
			if(this.checkForm(request,loginForm)){
				UserBase userBase = userManagerServ.getUserBaseByUserName(loginForm.getUserName());
				if(userBase!=null){
					//如果密码相等
					if(userBase.getPassword().equals(DesEncrypter.encrypt(loginForm.getPassword().trim()))){
						HttpSession session = request.getSession(true);
						session.setAttribute("UserBase", userBase);
						if(userBase.isAdmin()){
							toPage = "adminPage";
						}else{
							toPage = "customPage";
						}
					}else{
						request.setAttribute("msg", "密码不正确,请重新登陆!");
					}
				}
			}
			
		} catch (BusinessException e) {
			this.getLogger().error("用户登陆,数据库异常",e);
			toPage ="error";
		} catch (Exception e) {
			this.getLogger().error("用户登陆,数据库异常",e);
			toPage ="error";
		}
		
		return mapping.findForward(toPage);
	}
	
	
	/***********
	 * 注册
	 */
	public ActionForward regist(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		return null;
		
	}
	
	/***********
	 * 校验form
	 * @param request
	 * @return
	 */
	private boolean checkForm(HttpServletRequest request,LoginForm  loginForm) {
		boolean isok = true;
		if(loginForm.getUserName()!=null  && !loginForm.getUserName().trim().equals("")){
			
			
		}else{
			isok = false;
			request.setAttribute("msg", "用户名必须输入");
		}
		
		if(loginForm.getPassword()!=null  && !loginForm.getPassword().trim().equals("")){
			
		}else{
			isok = false;
			request.setAttribute("msg", "密码必须输入");
		}
		
		if(loginForm.getCheckCode()!=null  && !loginForm.getCheckCode().trim().equals("")){
			
			String httpVeryCode = (String)request.getSession().getAttribute(Globals.RANDOM_LOGIN_KEY);
			if(!loginForm.getCheckCode().equals(httpVeryCode)){
				isok = false;
				request.setAttribute("msg", "验证码不正确");
			}
			
		}else{
			isok = false;
		}
		
		return isok;
		
	}

	/********
	 * 登出
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward loginOut(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		   String toPage="loginPage";
		   if(request.getSession().getAttribute("UserBase")!=null){
			   request.getSession().removeAttribute("UserBase");
		   }
		   
		return mapping.findForward(toPage);
		
	}
			
			
			
	  
	  
	  
}
